﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseFunction;
using BaseFunction.DEncrypt;
using Maticsoft.Common;
using YY.Model;

namespace YY.Web.MyAgent
{
    public partial class AgentInfo : BaseFunction.LoginHelp
    {
        string defaultpassword = "0|0|0|0"; //默认显示密码
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initData();
            }
        }
        #region 赋值操作
        private void initData()
        {
            YY.Model.agent_users model_agent_users =
            base.GetSessionModel();
            txtTrueName.Text = model_agent_users.true_name.Trim();
            lblUserName.Text = model_agent_users.user_name.Trim();
            txtAddress.Text = model_agent_users.address;
            if (!string.IsNullOrEmpty(model_agent_users.password))
            {
                txtPassword.Attributes["value"] = txtPassword1.Attributes["value"] = defaultpassword;
            }

            if (!string.IsNullOrEmpty(model_agent_users.telphone) &&
                model_agent_users.telphone.Contains('-'))
            {
                txtTelphoneQh.Text = model_agent_users.telphone.Split('-')[0];
                txtTelphone.Text = model_agent_users.telphone.Split('-')[1];
            }
            else
            {
                txtTelphone.Text = model_agent_users.telphone;
            }
            txtMobile.Text = model_agent_users.mobile;
            txtImgUrl.Text = model_agent_users.head_img_url;
            txtEmail.Text = model_agent_users.e_mail;
            //txtContent.Value = model_suppliers_info.reason;
            txtDocuments1.Text = model_agent_users.documents1;
            txtDocuments2.Text = model_agent_users.documents2;
            txtDocuments3.Text = model_agent_users.documents3;
            txtDocuments4.Text = model_agent_users.documents4;
            txtIdNumber.Text = model_agent_users.id_number;
            rblGender.SelectedValue = model_agent_users.sex.ToString();
            //rblIsSubmit.SelectedValue = model_suppliers_info.is_submit.ToString();
            //HiddenField_City_Code.Value = model_agent_users.city;
            HiddenField_Area_Value.Value = model_agent_users.area;
        }
        #endregion
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!DoEdit())
            {
               BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"保存过程中发生错误！\"");
                return;
            }
            BaseFunction.MessageBox.ResponseScript(this.Page, string.Format("parent.jsprint(\"修改代理商信息成功！\",\"\")"));
        }
        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <returns></returns>
        private bool DoEdit()
        {
            bool result = false;
            YY.Model.agent_users getmodel_agent_users =
            base.GetSessionModel();
            //判断密码是否更改
            if (txtPassword.Text.Trim() != defaultpassword)
            {
                //获取用户已生成的salt作为密钥加密
                getmodel_agent_users.password = DESEncrypt.Encrypt(txtPassword.Text.Trim(), getmodel_agent_users.salt);
            }
            getmodel_agent_users.true_name = BaseFunction.StringHelp.GetSafeStr(txtTrueName.Text);
            getmodel_agent_users.user_name = BaseFunction.StringHelp.GetSafeStr(lblUserName.Text);
            getmodel_agent_users.address = BaseFunction.StringHelp.GetSafeStr(txtAddress.Text);

            if (string.IsNullOrEmpty(txtTelphoneQh.Text) && string.IsNullOrEmpty(txtTelphone.Text))
            {
                getmodel_agent_users.telphone = "";
            }
            else if (!string.IsNullOrEmpty(txtTelphoneQh.Text) && string.IsNullOrEmpty(txtTelphone.Text))
            {
                getmodel_agent_users.telphone = BaseFunction.StringHelp.GetSafeStr(txtTelphoneQh.Text) + "-";
            }
            else if (string.IsNullOrEmpty(txtTelphoneQh.Text) && !string.IsNullOrEmpty(txtTelphone.Text))
            {
                getmodel_agent_users.telphone = "-" + BaseFunction.StringHelp.GetSafeStr(txtTelphone.Text);
            }
            else
            {
                getmodel_agent_users.telphone = BaseFunction.StringHelp.GetSafeStr(txtTelphoneQh.Text) + "-" + BaseFunction.StringHelp.GetSafeStr(txtTelphone.Text);
            }

            getmodel_agent_users.mobile = BaseFunction.StringHelp.GetSafeStr(txtMobile.Text);
            getmodel_agent_users.head_img_url = BaseFunction.StringHelp.GetSafeStr(txtImgUrl.Text);
            getmodel_agent_users.e_mail = BaseFunction.StringHelp.GetSafeStr(txtEmail.Text);
            getmodel_agent_users.documents1 = BaseFunction.StringHelp.GetSafeStr(txtDocuments1.Text);
            getmodel_agent_users.documents2 = BaseFunction.StringHelp.GetSafeStr(txtDocuments2.Text);
            getmodel_agent_users.documents3 = BaseFunction.StringHelp.GetSafeStr(txtDocuments1.Text);
            getmodel_agent_users.documents4 = BaseFunction.StringHelp.GetSafeStr(txtDocuments2.Text);

            getmodel_agent_users.id_number = BaseFunction.StringHelp.GetSafeStr(txtIdNumber.Text);
            getmodel_agent_users.sex = int.Parse(BaseFunction.StringHelp.GetSafeStr(rblGender.SelectedValue));
            //getmodel_agent_users.is_submit = int.Parse(BaseFunction.StringHelp.GetSafeStr(rblIsSubmit.SelectedValue));
            var city_code = HiddenField_City_Code.Value;
            if (string.IsNullOrEmpty(city_code))
            {
                //.
            }
            else
            {
                if (city_code.Split(',').Length == 2)
                {
                    getmodel_agent_users.city = city_code.Split(',')[1];//城市编号 
                }
                else
                {
                    //MessageBox.ResponseScript(this.Page,"<script type='text/javascript'>alert('请选择所在的城市');</script>");
                    BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"请选择所在的城市！\"");
                    return false;
                }
            }
            getmodel_agent_users.area = HiddenField_Area_Value.Value;
      
            if (BaseFunction.StaticTableClass.agent_users_Help.bll_agent_users.Update(getmodel_agent_users))
            {
                AddAgentInfoLog(YYEnums.ActionEnum.Edit.ToString(), "修改代理商信息" + getmodel_agent_users.true_name); //记录日志
                return true;
            }
            return result;
        }
        /*代理商日志*/
        private bool AddAgentInfoLog(string action_type, string remark)
        {
            YY.Model.agent_login_log model_agent_login_log = new agent_login_log();
            model_agent_login_log.action_type = action_type;
            model_agent_login_log.remark = remark;
            model_agent_login_log.user_name = GetSessionModel().user_name;
            model_agent_login_log.user_id = GetSessionAgentId();
            model_agent_login_log.add_time = DateTime.Now;
            model_agent_login_log.login_ip = Request.UserHostAddress;
            int newId =
                BaseFunction.StaticTableClass.agent_login_log_Help.bll_agent_login_log.Add(model_agent_login_log);
            if (newId > 0)
            {
                return true;
            }
            return false;
        }
        #region 提交审核操作
        protected void btnSubmitAudit_Click(object sender, EventArgs e)
        {
            /*先保存，后提交*/
            if (!DoEdit())
            {
                BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"保存过程中发生错误！\"");
                return;
            }
            YY.Model.agent_users getmodel_agent_users =
            base.GetSessionModel();
            btnSubmitAudit.Visible = true;//显示审核按钮
            //getmodel_agent_users.is_submit = 1;//提交
            if (getmodel_agent_users.status == 1)//审核未通过 重新提交审核
            {
                getmodel_agent_users.status = 0;//在审核中
            }
            if (getmodel_agent_users.status == 2)
            {
                btnSubmitAudit.Visible = false;//已经审核通过 隐藏审核按钮
            }
            if (string.IsNullOrEmpty(getmodel_agent_users.frozen_capital))
            {
                BaseFunction.MessageBox.ParentJscriptMsg(this.Page, "尚未缴纳保证金！","PayDeposit.aspx");
                return;
            }
            bool isUpdate = BaseFunction.StaticTableClass.agent_users_Help.bll_agent_users.Update(getmodel_agent_users);
            if (!isUpdate)
            {
                BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"提交审核过程中发生错误！\",\"\"");
                return;
            }
            BaseFunction.MessageBox.ResponseScript(this.Page, string.Format("parent.jsprint(\"提交审核成功！\",\"\")"));
        }
        #endregion
    }
}